package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 护理项目Service实现类
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> list(String name, Integer pageNum, Integer pageSize, Integer status) {
        // 利用分页插件设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        // 调用mapper层分页条件查询
        Page<NursingProjectVo>  page = nursingProjectMapper.list(name,status);
        // 将结果传入PageResponse对象中
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void save(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);

        // 调用mapper层插入数据
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.seleteById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 修改护理
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 启用禁用护理项目
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);

        nursingProjectMapper.update(nursingProject);
    }
}